﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wormhole.Engine.Communication.Messages
{
	/// <author>Hannes Foulds</author>
	/// <date>13 October 2008</date>
	/// <summary>
	/// This class is used for messages relating to game actions.
	/// </summary>
	public class GameActionMessage : Message
	{
		#region Properties

		#region Game Action
		/// <summary>
		/// The action which the message is for.
		/// </summary>
		public Action.GameAction GameAction { get; set; }
		#endregion

		#endregion

		#region Constructor
		/// <summary>
		/// Initialze a new instance of the class.
		/// </summary>
		/// <param name="gameAction">The game action the message is for.</param>
		public GameActionMessage(Action.GameAction gameAction)
		{
			this.Action = Actions.GameAction;
			this.GameAction = gameAction;
		}

		/// <summary>
		/// Initailize a new instance of the class from its string representation.
		/// </summary>
		/// <param name="message">The string message.</param>
		public GameActionMessage(string message)
		{
			this.Action = Actions.GameAction;
			this.Parse(message);
		}
		#endregion

		#region To String
		/// <summary>
		/// Convert the message to a string that can be send via the communications channel.
		/// </summary>
		/// <returns>Returns the string representation of the message.</returns>
		public override string ToString()
		{
			return string.Format("{0}|{1}|{2}",
				EnumConvertor<Actions>.ToString(this.Action),
				EnumConvertor<Wormhole.Engine.Communication.Action.GameActionType>.ToString(this.GameAction.Action),
				this.EncodeString(this.GameAction.Target));
		}
		#endregion

		#region Parse
		/// <summary>
		/// Parse a received game list message.
		/// </summary>
		/// <param name="message">The string message to parse.</param>
		protected void Parse(string message)
		{
			string[] messageParts = message.Split('|');

			// create the card action
			this.GameAction = new Wormhole.Engine.Communication.Action.GameAction();
			this.GameAction.Action = EnumConvertor<Wormhole.Engine.Communication.Action.GameActionType>.FromString(messageParts[1]);
			this.GameAction.Target = this.DecodeString(messageParts[2]);
		}
		#endregion

	}
}
